home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xcmd / xrulesde.sit / Xrules™ Tutorial / card_9805.txt < prev    next >
Encoding:
Text File  |  1991-06-24  |  12.2 KB  |  178 lines

  1. -- card: 9805 from stack: in
  2. -- bmap block id: 10093
  3. -- flags: 4000
  4. -- background id: 7050
  5. -- name: 1-Forward Chaining
  6.  
  7.  
  8. -- part 1 (field)
  9. -- low flags: 01
  10. -- high flags: 0007
  11. -- rect: left=2 top=26 right=314 bottom=511
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 0
  15. -- font id: 22
  16. -- text size: 10
  17. -- style flags: 0
  18. -- line height: 13
  19. -- part name: doc
  20.  
  21.  
  22. -- part contents for card part 1
  23. ----- text -----
  24. In a forward chaining system, the inference engine begins with facts and moves toward a conclusion, or goal.  A forward chaining system works from a list of facts and their corresponding queries.  It will examine the rule base after each query to see if any of the rules can be evaluated.  It reaches a conclusion when enough facts are answered in a manner to cause one of the rules supplying a conclusion to fire.  The order of the queries and, within the queries, the order of the rules affect the order of the search, hence the order of the solutions.
  25.  
  26. The logic for forward chaining can be expressed as follows:
  27.  
  28. main:
  29.   set pointer to top of rule base
  30.   while facts with queries remain
  31.     get next fact
  32.     if fact not asserted then
  33.       prompt user and get answer
  34.       assert fact
  35.       process_rules(fact)
  36.     end if
  37.   end while
  38. end main
  39.  
  40. process rules(fact):
  41.   set pointer to top of rule base
  42.   while rules remain
  43.     get next rule
  44.     if rule uses fact then
  45.       evaluate rule
  46.       if rule evaluates true then
  47.         if rule asserts new fact then
  48.           assert new fact
  49.           process daemons(new fact)
  50.           process rules(new fact)
  51.         end if
  52.         if rule draws conclusion
  53.           announce conclusion
  54.         end if
  55.       end if
  56.     end if
  57.   end while
  58. end process rules
  59.  
  60. Note that the call to "process rules" within the routine "process rules" is a recursive call and that it passes in the new fact.  Thus, anytime that a fact is asserted when forward chaining, all rules using that fact are evaluated to determine whether a conclusion can be derived.  If all of the facts required to evaluate a rule are not asserted, then the inference engine skips the rule.
  61.  
  62. In our example, the list of facts and their corresponding queries are as shown below.  As you will see, the inference engine will examine the facts in the order listed.
  63.  
  64.   fact
  65.     passengers
  66.       "How many passengers can it carry?",
  67.     wheels
  68.       "How many wheels does it have?",
  69.     "used for cargo"
  70.       "Is it used to carry cargo?",
  71.     make
  72.       "What is the make of the vehicle?"
  73.  
  74. The next few paragraphs will walk you through the forward chaining inference process using our sample rule base. 
  75.  
  76. You may walk through this example using the shell. Go to the "tutorial shell" card.  Click on the "load RB" button to make certain that the rule base is loaded in the "Rule Base" field.  Next, if the "Forward Chain/Backward Chain" field does not show "Forward Chain", click on it to toggle it to "Forward Chain". 
  77.  
  78. If you would like to monitor the process, click on the "trace" check box to set trace on (an X will show in the box when it is on).  To step through the process, click on the "step" check box to set step on (an X will show in the box when it is on). 
  79.  
  80. Next, click on the "(re)start" button to start the session and answer the questions or follow the directions as they appear in the "Query" field.  In this case, you should enter carriage return in the "Answer" field after clicking on "(re)start". During the session you may toggle between this card and others in the stack.  The shell will retain its state when you toggle among cards; you do not have to start over each time that you return to the shell.
  81.  
  82. If you checked both trace and step, watch the "Trace" field to see the actions taken by the system.  The system will show the state of the stack in the "Stack" field and the facts and their values in the "Facts" field.
  83.  
  84. NOTE: The system handles daemons and traces actions with respect to them.  You will notice that whenever a fact is asserted, that fact is pushed on the stack while the system looks for daemons associated with it.  Therefore, the fact name will appear twice on the stack during that processing.  For this tutorial ignore these actions taken by the system concerning daemons.
  85.  
  86. The system begins with the stack empty, so it looks for the first fact having a query, finds fact "passengers", and pushes it on the stack.  It then prompts with the query, which is, "How many passengers can it carry?".  Answer "4" in the "Answer" field.  The system asserts the fact "passengers = 4" and, after processing daemons, searches the rule base for rules with clauses using fact 
  87. "passengers". 
  88.  
  89. The system finds the rule "Corvette", pushes it on the stack, and evaluates it.  Rule "Corvette" fails because fact "car" has not yet been asserted, so it is popped from the stack.
  90.  
  91. The system then resumes the search for rules using fact "passengers" and finds rule "Camaro", pushes it on the stack, and evaluates it.  Rule "Camaro" also fails because fact "car" has not been established, so it is popped from the stack. 
  92.  
  93. The system then resumes the search for rules using fact "passengers" and finds rule "car 1", pushes it on the stack, and attempts to evaluate it.  Rule "car 1" fails because fact "wheels" has not yet been asserted so rule "car 1" is popped from the stack. 
  94.  
  95. The system continues the search and finds that rule "car 3" uses fact 
  96. "passengers", pushes rule "car 3" on the stack, and attempts to evaluate it.  Rule "car 3" proves false because the clause does not match fact "passengers" so it is popped from the stack.  The clause appears true in the trace but, since there is a NOT preceding the clause, it actually proves false.
  97.  
  98. No more rules use fact "passengers", so it is popped from the stack.
  99.  
  100. The stack is now empty, so the system looks for the next fact having a query, finds fact "wheels", and pushes it on the stack.  It then prompts with the query, which is, "How many wheels does it have?".  Answer "4". The system asserts the fact "wheels = 4" and, after processing daemons, searches for a rule using fact 
  101. "wheels".
  102.  
  103. The system finds rule "car 1", pushes it on the stack, and attempts to evaluate it.  The clauses about wheels and passengers prove true, but the rule fails because fact "used for cargo" has not yet been asserted so rule "car 1" is  popped from the stack. 
  104.  
  105. The system then resumes the search for rules using fact "wheels" and finds rule 
  106. "car 2", pushes it on the stack, and attempts to evaluate it.  Rule "car 2" proves false because the clause does not match fact "wheels" so it is popped from the stack.  The clause appears true in the trace but, since there is a NOT preceding the clause, it actually proves false.
  107.  
  108. No more rules use fact "wheels", so it is popped from the stack.
  109.  
  110. The stack is now empty, so the system looks for the next fact having a query, finds fact "used for cargo", and pushes it on the stack. It then prompts with the query, which is, "Is it used to carry cargo?".  Answer "no".  The system asserts the fact "'used for cargo' = no" and, after processing daemons, searches for rules using fact "used for cargo". 
  111.  
  112. It finds rule "car 1" and pushes it on the stack.  This time it is able to completely evaluate rule "car 1" since facts "wheels", "passengers", and "used for cargo" have all been asserted.  Since the clauses match the rules, rule "car 1" fires and the "then" part of the rule is executed, asserting fact "car = true". When fact "car" is asserted, the system pushes it on the stack, processes daemons,  and searches for rules using it.
  113.  
  114. The system first finds "Corvette", pushes it on the stack, and evaluates it.  Rule "Corvette" fails because fact "make" has not been established, so it is popped from the stack.
  115.  
  116. The system resumes the search for rules using fact "car" and finds rule "Camaro", pushes it on the stack, and evaluates it.  It also fails to evaluate because fact "make" has not been established, so it is popped from the stack. 
  117.  
  118. The system continues searching for rules using fact "car", finds rule "Mustang", pushes it on the stack, and evaluates it.  It too fails to evaluate because fact "make" has not been established, so it is popped from the stack. 
  119.  
  120. Since no other rules use fact "car", it is popped from the stack.  Since rule 
  121. "car 1" is finished, it too is popped from the stack, leaving "used for cargo" on top of the stack.
  122.  
  123. The system then resumes the search for rules using fact "used for cargo" and finds rule "car 4", pushes it on the stack, and attempts to evaluate it.  Rule 
  124. "car 4" fails because the clause doesn't match fact "used for cargo" and it is popped from the stack.  The clause appears true in the trace but, since there is a "not" preceding the clause, it actually proves false.
  125.  
  126. Since no other rules use fact "used for cargo", it is popped from the stack.
  127.  
  128. The stack is now empty, so the system looks for the next fact with a query, finds fact "make", and pushes it on the stack. It then prompts with the query, which is, "What is the make of the vehicle?".  Answer "Chevy".  The system asserts fact "make = Chevy" and, after processing daemons, searches for rules using fact "make". 
  129.  
  130. It first finds rule "Corvette", pushes it on the stack, and evaluates it.  Rule 
  131. "Corvette" evaluates false because fact "passengers" does not match the clause, so it is popped from the stack. 
  132.  
  133. The system resumes the search for rules using fact "make" and finds rule "Camaro", pushes it on the stack, and evaluates it.  Rule "Camaro" evaluates true, so the system executes the "then" part of the rule and asserts fact "model = Camaro".  It pushes fact "model" on the stack, processes daemons, and searches for rules using it. 
  134.  
  135. It finds no rules using fact "model", so fact "model" is popped from the stack. 
  136.  
  137. The system then executes the next statement in the "then" part of rule "Camaro" and informs the user that "The car is a Camaro." 
  138.  
  139. If you elect to continue, the system pops rule "Camaro" from the stack.
  140.  
  141. The system then continues searching for rules using fact "make" and finds rule 
  142. "Mustang", pushes it on the stack, and evaluates it.  Rule "Mustang" proves false because fact "make" doesn't match the clause, so the system pops rule 
  143. "Mustang" from the stack. 
  144.  
  145. It then searches for more rules using fact "make" but finds none, so it pops fact "make" from the stack.  The system can go no further because there are no more facts having queries.
  146.  
  147. After walking through the tutorial for the above case, change the order of the fact declarations and watch the response.  Change the fact declaration section so that fact "make" precedes fact "passengers" as shown.
  148.  
  149.   fact
  150.     make
  151.       "What is the make of the vehicle?",
  152.     passengers
  153.       "How many passengers can it carry?",
  154.     wheels
  155.       "How many wheels does it have?",
  156.     "used for cargo"
  157.       "Is it used to carry cargo?"
  158.  
  159. Now click on "(re)start" and begin the session (do not click on "load RB" or it will restore the rule base to its original condition).  During the consultation, you will see that it asks about fact "make" first. 
  160.  
  161. Try the system again, reducing the number of rules.  Remember that we had to add rules "car 2", "car 3", and "car 4" to cover the false condition for fact "car".  We will now show an alternative to these rules.  Click on the "load RB" button to restore the original rule base.  Then delete the rules "car 2", "car 3", and 
  162. "car 4" in the rule base field and add the "else" clause to rule "car 1" as shown.
  163.  
  164.   rule "car 1"
  165.   if 
  166.     fact wheels = 4 and
  167.     fact passengers <= 4 and
  168.     fact "used for cargo" = no
  169.   then 
  170.     put true into fact car
  171.   else
  172.     put false into fact car
  173.  
  174. Run the system again (don't forget to "(re)start", and observe its actions.  You will find that, if you answer facts "wheels", "passengers", or "used for cargo" in a manner that will indicate that the vehicle is not a car, the system will execute the "else" part of the rule and set fact "car" to "false".  This achieves the same effect as having rules "car 2", "car 3", and "car 4".  You should note that this shortcut can cause problems later if you expand the knowledge in your system with respect to fact "car", unless you remember that you used the shortcut.  The purists have reasons for their views!
  175.  
  176. Feel free to modify the rule base on the "tutorial shell" card and experiment with forward chaining.  If you wish to restore the original sample rule base, simply click on the "load RB" button.
  177.  
  178.